package com.hanxing.kxcharge.module.operations.service.equipment;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.hanxing.kxcharge.module.operations.controller.admin.equipment.vo.*;
import com.hanxing.kxcharge.module.operations.dal.dataobject.equipment.EquipmentDO;
import com.hanxing.kxcharge.framework.common.pojo.PageResult;
import com.hanxing.kxcharge.framework.common.pojo.PageParam;
import com.hanxing.kxcharge.framework.common.util.object.BeanUtils;

import com.hanxing.kxcharge.module.operations.dal.mysql.equipment.EquipmentMapper;

import static com.hanxing.kxcharge.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.hanxing.kxcharge.module.operations.enums.ErrorCodeConstants.*;

/**
 * 电桩管理 Service 实现类
 *
 * @author 航兴充电
 */
@Service
@Validated
public class EquipmentServiceImpl implements EquipmentService {

    @Resource
    private EquipmentMapper equipmentMapper;

    @Override
    public Long createEquipment(EquipmentSaveReqVO createReqVO) {
        // 插入
        EquipmentDO equipment = BeanUtils.toBean(createReqVO, EquipmentDO.class);
        equipmentMapper.insert(equipment);
        // 返回
        return equipment.getId();
    }

    @Override
    public void updateEquipment(EquipmentSaveReqVO updateReqVO) {
        // 校验存在
        validateEquipmentExists(updateReqVO.getId());
        // 更新
        EquipmentDO updateObj = BeanUtils.toBean(updateReqVO, EquipmentDO.class);
        equipmentMapper.updateById(updateObj);
    }

    @Override
    public void deleteEquipment(Long id) {
        // 校验存在
        validateEquipmentExists(id);
        // 删除
        equipmentMapper.deleteById(id);
    }

    private void validateEquipmentExists(Long id) {
        if (equipmentMapper.selectById(id) == null) {
            throw exception(EQUIPMENT_NOT_EXISTS);
        }
    }

    @Override
    public EquipmentDO getEquipment(Long id) {
        return equipmentMapper.selectById(id);
    }

    @Override
    public PageResult<EquipmentDO> getEquipmentPage(EquipmentPageReqVO pageReqVO) {
        return equipmentMapper.selectPage(pageReqVO);
    }

}